package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import databaseInterface.CatalogManageInterface;

public class CatalogManage implements CatalogManageInterface{

	Statement stmt;
	DatabaseConnection db= new DatabaseConnection();
	
	public  void connect (){
        
         stmt =db.connect();
    }
    
	public void insert (String str){
    	 String insert ="insert into catalog  values ('"+str+"')";
         try {
			stmt.executeUpdate(insert);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    
   }
    
 
    
    
    
	public	void delete (String str ){
    	
    	String delete="delete from catalog  where catalog = '"+str+"'" ;
    	try {
			stmt.executeUpdate(delete);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
 
	public   ArrayList<String> search (String query){
    	ArrayList<String> tlist = new ArrayList<String> ();
     	try {
			ResultSet rs= stmt.executeQuery(query);
		
    	
    	while(rs.next()){
    		String str= rs.getString(1);
    	    tlist.add(str);
    	
    	}
    	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	return tlist;	
    }
 
  
	public   ArrayList<String> query (){
    	String query = "select * from catalog ";
    	ArrayList<String> tlist = search(query);
    	return tlist;	
    }


    public boolean exist(String cat){
    	String query = "select * from catalog where catalog = '"+cat+"'";
    	ArrayList<String> tlist =search(query);
    	if(tlist.isEmpty()) return false;
    	return true ;
    }

	public	void close(){
      db.close();
    }
   




}
